package com.ask.controller.admin;

import com.ask.dto.SetMealDTO;
import com.ask.dto.SetMealQueryDTO;
import com.ask.result.PageResult;
import com.ask.result.Result;
import com.ask.service.SetMealService;
import com.ask.vo.SetMealVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController("adminSetMealController")
@RequestMapping("/admin/setmeal")
@Api(tags = "套餐接口")
public class SetMealController {
    @Autowired
    private SetMealService setMealService;

    /***
     * 新增套餐
     * @param setMealDTO
     * @return
     */
    @PostMapping
    @ApiOperation("新增套餐")
    @CacheEvict(cacheNames = "setMealCache", key = "#setMealDTO.categoryId")
    public Result save(@RequestBody SetMealDTO setMealDTO){
        setMealService.save(setMealDTO);
        return Result.success();
    }

    /**
     * 根据id获取套餐及菜品信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result<SetMealVO> getByIdWithDishes(@PathVariable Long id) {
        SetMealVO setMealVO = setMealService.getByIdWithDishes(id);
        return Result.success(setMealVO);
    }

    /**
     * 套餐分页查询
     * @param setMealQueryDTO
     * @return
     */
    @GetMapping("/page")
    public Result<PageResult> pageQuery(SetMealQueryDTO setMealQueryDTO) {
        PageResult pageResult = setMealService.pageQuery(setMealQueryDTO);
        return Result.success(pageResult);
    }


    /**
     * 套餐起售/停售
     * @param status
     * @param id
     * @return
     */
    @PostMapping("/status/{status}")
    @CacheEvict(cacheNames = "setMealCache", allEntries = true)
    public Result startOrstop(@PathVariable Integer status, Long id) {
        setMealService.startOrstop(status, id);
        return Result.success();
    }

    /**
     * 修改套餐
     * @param setMealDTO
     * @return
     */
    @PutMapping
    @ApiOperation("修改套餐")
    @CacheEvict(cacheNames = "setMealCache", key = "#setMealDTO.categoryId")
    public Result update(@RequestBody SetMealDTO setMealDTO) {
        setMealService.updateWithDishes(setMealDTO);
        return Result.success();
    }


    /**
     * 删除套餐
     * @param ids
     * @return
     */
    @DeleteMapping
    @CacheEvict(cacheNames = "setMealCache", allEntries = true)
    public Result delete(@RequestParam List<Long> ids) {
        setMealService.delete(ids);;
        return Result.success();
    }
}
